package com.passionfruit.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.passionfruit.common.utils.R;
import com.passionfruit.module.entity.BlogArticleCate;
import com.passionfruit.module.entity.SysMenu;
import com.passionfruit.module.service.IBlogArticleCateService;
import com.xiaoleilu.hutool.convert.Convert;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;




/**
 * 
 * 
 * @author weirdor
 * @email 179946422@qq.com
 * @date 2017-11-06 20:05:15
 */
@RestController
@RequestMapping("/module/blogarticlecate")
public class BlogArticleCateController {
	@Autowired
	private IBlogArticleCateService blogArticleCateService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("module:blogarticlecate:list")
	public R list(@RequestParam Map<String, Object> params){
		EntityWrapper<BlogArticleCate> wrapper = new EntityWrapper<BlogArticleCate>();

		Page<BlogArticleCate> pageUtil = blogArticleCateService.selectPage(new Page<BlogArticleCate>(Convert.toInt(params.get("page")), Convert.toInt(params.get("limit"))), wrapper);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/all")
	public List<BlogArticleCate> list() {
		EntityWrapper<BlogArticleCate> wrapper = new EntityWrapper<BlogArticleCate>();
		List<BlogArticleCate> blogArticleCateList = blogArticleCateService.queryList(new HashMap<String, Object>());

		return blogArticleCateList;
	}


	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public R select() {
		//查询列表数据
		List<BlogArticleCate> menuList = blogArticleCateService.queryList(new HashMap<String, Object>());
		menuList.forEach(k->{
			k.setName(k.getCateTitle());
		});
		//添加顶级菜单
		BlogArticleCate root = new BlogArticleCate();
		root.setId(0);
		root.setName("一级栏目");
		root.setCatePid(-1);
		root.setOpen(true);
		menuList.add(root);

		return R.ok().put("menuList", menuList);
	}


	/**
	 * 列表
	 */
	@RequestMapping("/lists")
	@RequiresPermissions("module:blogarticlecate:list")
	public R lists(){
		EntityWrapper<BlogArticleCate> wrapper = new EntityWrapper<BlogArticleCate>();


		return R.ok().put("page", blogArticleCateService.selectList(wrapper));
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("module:blogarticlecate:info")
	public R info(@PathVariable("id") Integer id){
		BlogArticleCate blogArticleCate = blogArticleCateService.selectById(id);
		
		return R.ok().put("blogArticleCate", blogArticleCate);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("module:blogarticlecate:save")
	public R save(@RequestBody BlogArticleCate blogArticleCate){
		blogArticleCateService.insert(blogArticleCate);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("module:blogarticlecate:update")
	public R update(@RequestBody BlogArticleCate blogArticleCate){
		blogArticleCateService.updateById(blogArticleCate);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(long menuId){
		blogArticleCateService.deleteBatchIds(CollectionUtil.newArrayList(menuId));
		
		return R.ok();
	}
	
}
